import React from 'react'

// 获取某个query参数
React.Component.prototype.getQueryData = function (name) {
  const reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i')
  const r = this.props.location.search.substr(1).match(reg)
  return r ? unescape(r[2]) : null
}

// 获取全部query参数
React.Component.prototype.getQueryDataAll = function () {
  const search = this.props.location.search
  const qr = search.length ? search.substring(1) : ''
  const parmas = {}
  const items = qr.length ? qr.split('&') : []
  const leng = items.length
  for (let i = 0; i < leng; i++) {
    const item = items[i].split('=')
    const name = decodeURIComponent(item[0])
    const value = decodeURIComponent(item[1])
    if (name.length > 0) {
      parmas[name] = value
    }
  }
  return parmas
}